package robot.map;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import robot.Const;
import robot.Robot;
import robot.RobotConsole;


public abstract class RobotMap extends JPanel {

	private static final long serialVersionUID = 8860063295298670697L;

	private int width = 1000;
	private int height = 1000;
	private byte[][] map;

	protected RobotConsole robotConsole;
	protected Robot robot;

	public RobotMap(RobotConsole robotConsole, Robot robot) {
		super();
		this.robotConsole = robotConsole;
		this.robot = robot;
		setPreferredSize(new Dimension(width, height));

		map = new byte[width][height];
		for (int i = 0 ; i < width ; i++) {
			for (int j = 0 ; j < width ; j++) {
				if (i==0 || i==0 || j==width || j==width) {
					map[i][j] = 1;
				}
				else {
					map[i][j] = 0;
				}
			}
		}
	}

	public byte[][] getMap() {
		return map;
	}

	public void setMap(byte[][] map) {
		this.map = map;
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);


		for (int i = 0 ; i < width ; i++) {
			for (int j = 0 ; j < width ; j++) {

				// empty space
				g.setColor(Const.MAP_NOTHING);
				if (map[i][j] == 0) {
					g.drawLine(i, j, i+1, j+1);
				}

				// objects
				g.setColor(Const.MAP_OBJECT);
				if (map[i][j] == 1) {
					g.drawLine(i, j, i+1, j+1);
				}
				
				// unknown
				g.setColor(Const.MAP_UNKNOWN);
				if (map[i][j] == 2) {
					g.drawLine(i, j, i+1, j+1);
				}
				
				// unknown
				g.setColor(Const.MAP_CLEAN);
				if (map[i][j] == 3) {
					g.drawLine(i, j, i+1, j+1);
				}
			}
		}

		robot.paintRobot((Graphics2D)g);
	}

	public void addRectangle(ObjectType objectType, int x, int y, int w, int h) {
		for (int i = 0 ; i < w ; i++) {
			for (int j = 0 ; j < h ; j++) {
				addPoint(objectType, i + x, j + y);
			}
		}
	}

	public void addPoint(ObjectType objectType, int x, int y) {
		if (x < 1000 && y < 1000 && x >= 0 && y >= 0) {
			map[x][y] = (byte) objectType.getIndex();
		}
		else {
			System.out.println("Przekroczono zakres współrzędnych mapy: "
					+ x + " - " + y);
		}
	}

}
